﻿package  {
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import com.flashdynamix.utils.SWFProfiler;
	
	
	public class GameBase extends MovieClip {
		public static var instance:GameBase;
		//global vars
		public var gameInfo:Object;
		//work vars
		private var fStage:Boolean = false;
		private var fNetwork:Boolean = false;
		private var fLoaded:Boolean = false;
		private var net_base:Network;
		//game content;
		
		//Buttons
		
		public var rootAlert:BaseAlert;
		public var game_main:ControllerReels;
		
		
		
		
		
		public function GameBase() {
			super();
			GameBase.instance = this;
			
			
			
			

			this.loaderInfo.addEventListener(Event.COMPLETE, this.hComplete);
			this.addEventListener(Event.ADDED_TO_STAGE, this.hOnStage);

			//this.net_base = new Network();
			//this.net_base.sendRequest({'action':'init','game':'newfarm'}, this.hNetSuccess, this.hNetError);
		}
		
		
		
		private function buildGame():void
		{
			//if(!this.fNetwork || !this.fStage || !this.fLoaded)
				//return;
			//TODO: construct game;
			
			//game_main = new ControllerReels();
			//this.addChild(game_main);
			//SWFProfiler.init(stage, this);
		}
		
		private function hNetSuccess(obj:Object):void
		{
			this.gameInfo = obj;
			if(this.gameInfo.result == 'OK')
			{
				this.fNetwork = true;
				this.buildGame();
				return;
			}
			//TODO: display error Alert;
			this.rootAlert = new BaseAlert();
			this.rootAlert.x = this.stage.stageWidth/2 - this.rootAlert.width/2;
			this.rootAlert.y = this.stage.stageHeight/2 - this.rootAlert.height/2;
			this.addChild(this.rootAlert);
			this.rootAlert.showAlert("Network Error",'Сервер вернул неверные данные',function(){ this.visible = false; },true);
		}
		
		private function hNetError(err:String):void
		{
			//TODO: display error Alert;
			this.rootAlert = new BaseAlert();
			this.rootAlert.x = this.stage.stageWidth/2 - this.rootAlert.width/2;
			this.rootAlert.y = this.stage.stageHeight/2 - this.rootAlert.height/2;
			this.addChild(this.rootAlert);
			this.rootAlert.showAlert("Network Error",'Ошибка соединения с сервером',function(){ this.visible = false; },true);
		}
		
		private function hComplete(e:Event):void
		{
			this.loaderInfo.removeEventListener(Event.COMPLETE, this.hComplete);
			this.fLoaded = true;
			this.buildGame();
		}
		
		private function hOnStage(e:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE, this.hOnStage);
			this.fStage = true;
			this.buildGame();
		}
		
	}
	
}
